package com.hn.shopadmin.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hn.kiss.modules.sys.shopcommon.common.ParmException;
import com.hn.kiss.modules.sys.shopdb.entity.DictDetail;
import com.hn.kiss.modules.sys.shopdb.entity.DictDetailExample;
import com.hn.kiss.modules.sys.shopdb.mapper.DictDetailMapper;
import com.hn.shopadmin.service.DictDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class DictDetailServiceimpl implements DictDetailService {
    @Autowired
    DictDetailMapper dictDetailMapper;

    @Override
    public int add(DictDetail dictDetail) throws ParmException {
        checkDictDetail(dictDetail);

        dictDetail.setState(1);
        return dictDetailMapper.insert(dictDetail);
    }

    @Override
    public int update(DictDetail dictDetail) throws ParmException {
        DictDetail byId = getById(dictDetail.getTypeCode(), dictDetail.getDictCode());
        //与原name不想等,校验名称是否存在
        if (!dictDetail.getDictName().equals(byId.getDictName())){
            boolean b = checkDictName(dictDetail.getTypeCode(), dictDetail.getDictName());
            if (b){
                throw new ParmException("字典名称已存在");
            }
        }
        return dictDetailMapper.updateByPrimaryKeySelective(dictDetail);
    }

    @Override
    public int start(String typeCode, String dictCode) {
        DictDetail dictDetail = new DictDetail();
        dictDetail.setTypeCode(typeCode);
        dictDetail.setDictCode(dictCode);
        dictDetail.setState(1);
        return dictDetailMapper.updateByPrimaryKeySelective(dictDetail);
    }

    @Override
    public int stop(String typeCode, String dictCode) {
        DictDetail dictDetail = new DictDetail();
        dictDetail.setTypeCode(typeCode);
        dictDetail.setDictCode(dictCode);
        dictDetail.setState(0);
        return dictDetailMapper.updateByPrimaryKeySelective(dictDetail);
    }

    @Override
    public DictDetail getById(String typeCode, String dictCode) {
        return dictDetailMapper.selectByPrimaryKey(typeCode, dictCode);
    }

    @Override
    public List<DictDetail> listAll(String typeCode) {
        DictDetailExample dictDetailExample = new DictDetailExample();
        dictDetailExample.createCriteria().andTypeCodeEqualTo(typeCode);

        return dictDetailMapper.selectByExample(dictDetailExample);
    }

    @Override
    public PageInfo<DictDetail> listPage(Integer page, Integer limit, String typeCode, String keywork, Integer state) {
        PageHelper.startPage(page, limit);
        if (keywork == null){
            keywork = "%%";
        }else{
            keywork = "%" + keywork.trim() + "%";
        }


        DictDetailExample dictDetailExample = new DictDetailExample();
        // (typeCode && state && dictName)
        DictDetailExample.Criteria criteria = dictDetailExample.createCriteria();
        if ( ! StringUtils.isEmpty(typeCode)){
            criteria.andTypeCodeEqualTo(typeCode);
        }
        if ( ! StringUtils.isEmpty(state)){
            criteria.andStateEqualTo(state);
        }
        criteria.andDictNameLike(keywork);


        // ||(typeCode && state && dictCode)
        DictDetailExample.Criteria criteria1 = dictDetailExample.or();
        if ( ! StringUtils.isEmpty(typeCode)){
            criteria1.andTypeCodeEqualTo(typeCode);
        }
        if ( ! StringUtils.isEmpty(state)){
            criteria1.andStateEqualTo(state);
        }
        criteria1.andDictCodeLike(keywork);


        //条件(typeCode && state && dictName) || (typeCode && state && dictCode)
        List<DictDetail> dictDetails = dictDetailMapper.selectByExample(dictDetailExample);

        return new PageInfo<DictDetail>(dictDetails);
    }

    /**
     *校验字典明细是否存在
     * @param dictDetail
     * @return
     * @throws ParmException
     */
    boolean checkDictDetail(DictDetail dictDetail) throws ParmException {
        DictDetail dictDetail1 = dictDetailMapper.selectByPrimaryKey(dictDetail.getTypeCode(), dictDetail.getDictCode());
        if (dictDetail1 != null){
            throw new ParmException("字典代码已存在");
        }

        boolean b = checkDictName(dictDetail.getTypeCode(), dictDetail.getDictName());
        if (b){
            throw  new ParmException("字典名称已存在");
        }
        return true;

    }


    /**
     * 校验字典名称在该类中是否存在
     * @param typeCode
     * @param dictName
     * @return
     */
    boolean checkDictName(String typeCode, String dictName){
        DictDetailExample dictDetailExample = new DictDetailExample();
        dictDetailExample.createCriteria()
                .andTypeCodeEqualTo(typeCode)
                .andDictNameEqualTo(dictName);
        List<DictDetail> dictDetails = dictDetailMapper.selectByExample(dictDetailExample);

        if (dictDetails == null || dictDetails.size() == 0){
            return false;
        }
        return true;
    }

}
